package com.etoak.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.etoak.common.core.enums.OrderStateEnum;
import com.etoak.entity.Order;
import com.etoak.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CancalService {

    @Autowired
    OrderMapper orderMapper;

    @RabbitListener(queues = "order.quque")
    public void get(String orderNum){
        System.out.println(orderNum);
        log.info("{}->",orderNum);

        Order order = this.getOrder(orderNum);
        if(ObjectUtils.isNotEmpty(order) && order.getOrderState().equals(Integer.parseInt(OrderStateEnum.NEW.getState()))) {
            order.setOrderState(Integer.valueOf(OrderStateEnum.CANCEL.getState()));
            order.setCancelTime(DateUtil.now());
            order.setCancelReason("超时未支付,自动取消!");

            orderMapper.updateById(order);

        }
    }

    private Order getOrder(String orderNum){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNum);
        return orderMapper.selectOne(queryWrapper);
    }
}

